/**
 * File with ajax-calls
 * The Artist and releaseinfo are from http://MusicBrainz.org
 * The Covers are from http://last.fm/ //audioscrobbler
 * The Ticket-info is from http://sonicliving.com
 * The ip-adresses and location nfo is from http://hostip.info
 * 
 * The keys for last.fm and sonicLiving may only be used for this application.
 * 
 * @author Erwin Goossen
 * 
*/

/**
 * Get image with audioscrobbler (service of last.fm)
 * @param art artistname
 * @param alb releasename
 * @param mb_id musicbrainz_id of the release(not implemengted yet)
 * @return none url will directly set as src of image
*/
function getImage(art,alb,mb_id) {
	getUrl = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist='+art+'&album='+alb;
	$.ajax({
		type: 'GET',
		url: getUrl,
		dataType: 'xml',
		success: function($xml){
			$xml = $($xml);
			imgUrl = '';
			curRelease.cover = '';
			var i = 0;
			$xml.find('image').each(
			function(){
				var size = $(this).attr('size');
				if (size == 'large') {
					curRelease.cover = $(this).text();
				}
			});
			if (curRelease.cover == '') {
				curRelease.cover = 'lib/img/nopic.png';
			}
			$('#cover').attr('src', curRelease.cover);
		},
		error: function(){
	  		$('#cover').attr('src', 'lib/img/nopic.png');
			showMessage('Error','Fout bij aanvraag gegevens\nControlleer de gegevens');
		}
	}
	);
} 

/**
 * Get data of all releases or the given release of given artist
 * @return none data wil directly be viewable in html whitin a releaselist
*/
function getData() {
	getUrl = 'http://musicbrainz.org/ws/1/release/?type=xml&limit=75&artist='+$('#inpArtist').val()+'&title='+$('#inpAlbum').val();
	$.ajax({
		type: 'GET',
		url: getUrl,
		dataType: 'xml',
		success: function($xml){
			$xml = $($xml);
			release = '';
			var i = 0;
			var style = 'odd';
			$xml.find('release').each(
			function(){
				i++;
				var rel = $(this).attr('ext:score');
				if (rel == 100) {
					style = 'odd';
					if (i%2 == 0){
						style = 'even';
					}
					var xmlTitle = $(this).find('title').text();
					var xmlArtist = $(this).find('artist').find('name').text();
					release += '<div class="releaseItem ' + style + '" id="' + $(this).attr('id') + '">' + xmlArtist+': '+xmlTitle+'</div>'
				}
			});
			if (release == '') {
		  		$('#cover').attr('src', 'lib/img/nopic.png');
				showMessage('Resultaat','Geen release gevonden');
			}
			$('#release').html(release);
	  		$('#cover').attr('src', 'lib/img/nopic.png');
			$('.releaseItem').click(
			function(){
				getRelease($(this).attr('id'));
			});
		},
 		error: function(){
	  		$('#cover').attr('src', 'lib/img/nopic.png');
			showMessage('Error','Fout bij aanvraag gegevens\nControlleer de gegevens');
		}
	}
	);
}

/**
 * Get data af the release selected from the releaselist
 * @return none data wil directly be viewable in html whitin a releaselist
*/
function getRelease(mb_id) {
	var release = '';
	getUrl = 'http://musicbrainz.org/ws/1/release/' + mb_id + '?type=xml&inc=artist+tracks+counts+release-events';
	var i = 0;
	//songs.clear();
	$.ajax({
		type: 'GET',
		url: getUrl,
		dataType: 'xml',
		success: function($xml){
			$xml = $($xml);
			$xml.find('release').each(
			function(){
				curRelease.artist = $(this).find('artist').find('name').text();
				curRelease.artist = replaceAll(curRelease.artist,"'","&acute;");
				curRelease.art_from = $(this).find('artist').find('life-span').attr('begin');
				curRelease.art_md_id = $(this).find('artist').attr('id');
				curRelease.album  = $(this).find('title:first').text();
				curRelease.album = replaceAll(curRelease.album,"'","&acute;");
				var year_get = $(this).find('release-event-list').find('event').attr('date');
				air.trace(year_get);
				if (year_get != undefined) {
				  curRelease.albumYear = getYear(year_get);
				}
				else {
					curRelease.albumYear = 'nnb';
				}
				  
				curRelease.alb_mb_id = $(this).attr('id');
				getImage(curRelease.artist,curRelease.album,curRelease.alb_mb_id);
				release += releaseHead(false);
				// Found artist and album, now handle the titles
				curRelease.songs = []; //reset array
				$xml.find('track').each(
				function(){
					var xmlTitle = $(this).find('title').text();
					var xmlTime = calcTime($(this).find('duration').text());
					curRelease.songs[i] = [];
					curRelease.songs[i][0] = replaceAll(xmlTitle,"'","&acute;");
					curRelease.songs[i][1] = xmlTime;
					i++;
				});
					release += showSongList();
				showSongList();
				if (release == '') {
					showMessage('Resultaat','Geen tracks gevonden');
				}
				$('#release').html(release);
			});			
		},	
 		error: function(){
	  		$('#cover').attr('src', 'lib/img/nopic.png');
			showMessage('Error','Fout bij aanvraag gegevens\nControlleer de gegevens');
		}
	});
}

function getConcertStatus(artist,id) {
	air.trace(id);
	var gig = false;
	var ip = configFile.ip;
	getUrl = 'http://api.sonicliving.com/api/?artists[]='+artist+'&ip_loc[]='+ip+'&key=7219da7e';
//	getUrl = 'testData/' + artist +'.json';
	$.getJSON(getUrl,function(json){
		var country = '';
		var gigid = '#'+ id;
		for (i = 0; i < json.length; i++) {
			country = json[i][0].venue_country;
			if (country == configFile.country) {
				$(gigid).removeClass('hide');
				$(gigid).mouseover()
			}
		}
	});
	return gig;
} 

function getConcertInfo(art) {
	var ip = configFile.ip;
	var gig = '';
	var artist = art;
	getUrl = 'http://api.sonicliving.com/api/?artists[]='+artist+'&ip_loc[]='+ip+'&key=7219da7e';
//	getUrl = 'testData/' + artist +'.json';
	$.getJSON(getUrl,function(json){
		var country = '';
		var gigCount = 0;
		gig = '<div class="gigInfo">';
		for (i = 0; i < json.length; i++) {
			country = json[i][0].venue_country;
			if (country == configFile.country) {
				gig += (gigCount==0 ? '' : '<hr />');
				gig += json[i][0].event_date + '<br />';
				gig += json[i][0].venue_name + '<br/>';
				gig += json[i][0].venue_city + '<br />';
				gig += '<a href="' + json[i][0].links[0].link_url +'">info</a>';
				gigCount ++;
			}
		}
		gig += '</div>'
		showMessage('Concerten ', gig);
	});
} 


function getLocation() {
	var ip = '';
	var location = '';
	var country = ''
	getUrl = 'http://api.hostip.info/';
	$.ajax({
		type: 'GET',
		async: false,
		url: getUrl,
		dataType: 'xml',
		success: function($xml){
			$xml = $($xml);
			ip = $xml.find('ip').text();
			configFile.ip = ip;
			location 	= $xml.find('gml:name').text();
			configFile.location = location;
			country	= $xml.find('countryAbbrev').text();
			configFile.country = country;
		},
		error: function(){
	  		$('#cover').attr('src', 'lib/img/nopic.png');
			showMessage('Error','Fout bij aanvraag gegevens\nControlleer de gegevens');
		}
	});
} 

function getLyric(artist, title) {
	var key 	= 'd19b4309ce11fbbcb-temporary.API.access';
	var lyric 	= '';
	var status 	= '';
	getUrl = 'http://api.lyricsfly.com/api/api.php?i=' + key + '&a=' + artist + '&t=' + title;
	$.ajax({
		type: 'GET',
		url: getUrl,
		async: false,
		dataType: 'xml',
		success: function($xml){
			$xml = $($xml);
			status = $xml.find('status').text();
			if (status == '200' || status == '300') {
				$xml.find('sg').each(
				function() {
					if ($(this).find('al').text().toUpperCase() == curRelease.album.toUpperCase()) {
						lyrics = [];
						lyrics[0] = curRelease.artist;
						lyrics[1] = $(this).find('tt').text();
						lyrics[2] = $(this).find('tx').text();
						lyrics[3] = $(this).find('cs').text();
						lyrics[4] = $(this).find('id').text();
					}
				});
			}
			else {
				lyrics = [];
				lyrics[0] = false;
				var messageGeenText = '<h2>Tekst niet gevonden</h2><a href="http://lyricsfly.com/submit/">toevoegen</a>';
				
				showMessage('Melding',messageGeenText);
			}
		},
		error: function(){
			showMessage('Error','Geen songtekst gevonden');
		}
	});
} 